import request from '@/utils/request'
import Qs from 'qs'
export function list(query) {
  return request({
    method: 'get',
    params: {
      _gp: 'admin.usercard',
      _mt: 'list',
      ...query
    }
  })
}

export function save(data) {
  return request({
    method: 'post',
    data: Qs.stringify({
      _gp: 'admin.usercard',
      _mt: 'create',
      userCardDO: JSON.stringify(data)
    })
  })
}

export function update(data) {
  return request({
    method: 'post',
    data: Qs.stringify({
      _gp: 'admin.usercard',
      _mt: 'update',
      ...data
    })
  })
}

export function del(data) {
  return request({
    method: 'post',
    data: Qs.stringify({
      _gp: 'admin.usercard',
      _mt: 'delete',
      ...data
    })
  })
}

export function active(data) {
  return request({
    method: 'post',
    params: {
      _gp: 'admin.usercard',
      _mt: 'rechargeMoney',
      id: data.id
    }
  })
}

export function detail(data) {
  return request({
    method: 'post',
    data: Qs.stringify({
      _gp: 'admin.usercard',
      _mt: 'detail',
      ...data
    })
  })
}

export function activationCard(data) {
  return request({
    method: 'post',
    data: Qs.stringify({
      _gp: 'admin.usercard',
      _mt: 'activationCard',
      ...data
    })
  })
}

export function getExcelInfo(data) {
  return request({
    method: 'post',
    params: {
      _gp: 'admin.usercard',
      _mt: 'queryToExcel',
      ...data
    }
  })
}

export function monthSummary(data) {
  return request({
    method: 'post',
    params: {
      _gp: 'admin.usercard',
      _mt: 'monthSummary',
      ...data
    }
  })
}

export function updateCard(data) {
  return request({
    method: 'post',
    params: {
      _gp: 'admin.usercard',
      _mt: 'updateCard',
      ...data
    }
  })
}cardBatchUpdate

export function cardBatchUpdate(data) {
  return request({
    method: 'post',
    params: {
      _gp: 'admin.usercard',
      _mt: 'cardBatchUpdate',
      ...data
    }
  })
}

export function importExcel(data, cardId) {
  const formData = new FormData()
  formData.append('file', data)
  return request({
    url: process.env.HOST + '/excel/userCardImport/' + cardId,
    processData: false,
    contentType: false,
    method: 'post',
    data: formData
  })
}

export function exportToExcel(cardNo, username, phone) {
  username = username || ''
  phone = phone || ''
  const exportToExcelHref = process.env.HOST + '/excel/exportUserCardDistribution'
  const a = document.createElement('a')
  a.setAttribute('href', encodeURI(exportToExcelHref + '?cardNo=' + cardNo + '&username=' + phone + '&phone=' + phone))
  a.click()
  a.remove()
}

export function exportUserCardToExcel() {
  const exportToExcelHref = process.env.HOST + '/excel/exportCard'
  const a = document.createElement('a')
  a.setAttribute('href', encodeURI(exportToExcelHref))
  a.click()
  a.remove()
}
